<?php
/**
 * @copyright  Copyright  LexisNexis, a division of Reed Elsevier Inc. All rights reserved.
 */
namespace test\sdk;

error_reporting(E_ALL);
require_once dirname(__FILE__).'/../vendor/autoload.php';
require_once dirname(__FILE__).'/../../src/sdk/UsageClient.php';

use sdk\UsageClient;
use PHPUnit\Framework\TestCase;

class UsageClientTest extends TestCase {
    //phpunit --verbose --debug --colors --filter testPushQueue UsageClientTest.php
    public function testPushQueue() {
        $ret = UsageClient::instance()->log('test', 'test.test', array('message' => 'hello word!'));
        $this->assertEquals($ret, true);
    }

    //phpunit --verbose --debug --colors --filter testGetClassName UsageClientTest.php
    public function testGetClassName() {
        $url = 'https://dev.lexisnexis.com.cn/topic/landing.php?tps=ep';
        $class = UsageClient::instance()->getClassName($url);
        $this->assertEquals($class, 'topic.landing.tps_ep');

        $url = 'https://dev.lexisnexis.com.cn';
        $class = UsageClient::instance()->getClassName($url);
        $this->assertEquals($class, 'index');

        $url = 'https://dev.lexisnexis.com.cn/user.php?action=logout';
        $class = UsageClient::instance()->getClassName($url);
        $this->assertEquals($class, 'user');

        $url = 'https://dev.lexisnexis.com.cn/test1';
        $class = UsageClient::instance()->getClassName($url);
        $this->assertEquals($class, 'test1');

        $url = 'https://dev.lexisnexis.com.cn/test1/test2.php';
        $class = UsageClient::instance()->getClassName($url);
        $this->assertEquals($class, 'test1.test2');

        $url = 'https://dev.lexisnexis.com.cn/test1/test2.php?test3=test4';
        $class = UsageClient::instance()->getClassName($url);
        $this->assertEquals($class, 'test1.test2');

        $url = 'https://dev.lexisnexis.com.cn/index/index.php';
        $class = UsageClient::instance()->getClassName($url);
        $this->assertEquals($class, 'index.index');

        $url = 'https://dev.lexisnexis.com.cn/index.php';
        $class = UsageClient::instance()->getClassName($url);
        $this->assertEquals($class, 'index');

        $url = "https://dev.lexisnexis.com.cn/topic/?tps=ep&&crid=c76091fe-9089-e78b-bf28-641a31fdf5dd&prid=10df4520-586c-297c-8e2c-83824009102b";
        $class = UsageClient::instance()->getClassName($url);
        //error: topic..index
        $this->assertEquals($class, 'topic.tps_ep');
    }

    public function testReplaceUrl() {
        $url = "https://dev.lexisnexis.com.cn/topic/?tps=ep";
        $class = UsageClient::instance()->replaceUrl($url);
        $this->assertEquals($class, $url);

        $url = "https://dev.lexisnexis.com.cn/topic/?tps=ep";
        $class = UsageClient::instance()->replaceUrl($url, '1', '2');
        $this->assertEquals($class, $url . '&prid=1&crid=2');

        $url = "https://dev.lexisnexis.com.cn/topic/?tps=ep";
        $class = UsageClient::instance()->replaceUrl($url, '1', '2', array(), '#__jump_url__');
        $this->assertEquals($class, $url . '&prid=1&crid=2#__jump_url__');

        $url = "https://dev.lexisnexis.com.cn/topic/?tps=ep";
        $class = UsageClient::instance()->replaceUrl($url, '', '', array(), '#__jump_url__');
        $this->assertEquals($class, $url . '#__jump_url__');

        $url = "/message/?topic=1&message=55So5oi35ZCN5oiW5a%2BG56CB6ZSZ6K%2Bv&eng=0&url=%2Fhome";
        $class = UsageClient::instance()->replaceUrl($url, '', '', array(), '#__jump_url__');
        var_dump($class);
        $this->assertEquals($class, $url . '#__jump_url__');
    }

    //phpunit --verbose --debug --colors --filter testReplaceUrlBug UsageClientTest.php
    public function testReplaceUrlBug() {
        $url = 'http://dev.lexisnexis.com.cn/translate';
        $class = UsageClient::instance()->replaceUrl($url);
        $this->assertEquals($class, $url);

        $url = 'http://dev.lexisnexis.com.cn/translate/';
        $class = UsageClient::instance()->replaceUrl($url);
        $this->assertEquals($class, $url);

        $url = 'http://dev.lexisnexis.com.cn/translate/?prid=a&crid=b';
        $class = UsageClient::instance()->replaceUrl($url, 'a', 'b');
        $this->assertEquals($class, $url);

        $url = 'http://dev.lexisnexis.com.cn/translate/?act-login&prid=a&crid=b';
        $class = UsageClient::instance()->replaceUrl($url);
        $this->assertEquals($class, $url);

        $url = 'http://dev.lexisnexis.com.cn/translate/?act-login';
        $class = UsageClient::instance()->replaceUrl($url);
        $this->assertEquals($class, $url);

        $url = 'http://dev.lexisnexis.com.cn/translate/?act-login=';
        $class = UsageClient::instance()->replaceUrl($url);
        $this->assertEquals($class, $url);

        $url = 'http://dev.lexisnexis.com.cn/translate/?act-login=a';
        $class = UsageClient::instance()->replaceUrl($url);
        $this->assertEquals($class, $url);

        $url = 'http://dev.lexisnexis.com.cn/translate/?act-login=a&prid=b&crid=c';
        $class = UsageClient::instance()->replaceUrl($url);
        $this->assertEquals($class, $url);
    }
}